package com.java.grammer.arrays;

/**
 * @author luzhikun
 * @date 2020/11/29- 21:23
 */
/*int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
 *要求将以上数组中的0项去掉，将不为0的值存入一个新的数组，生成新的数组为:
 *int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5};
 */
public class OldArrays_2 {
    public static void main(String[] args) {
        int[] oldArr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
        int[] newArr = new int[oldArr.length];
        for (int i = 0; i < oldArr.length; i++) {
            if (oldArr[i] == 0) {
                if (oldArr[i + 1] != 0) {
                    oldArr[i] = oldArr[i + 1];
                } else if (i + 2 != 0) {
                    oldArr[i] = oldArr[i + 2];
                }
            }
        }
        System.out.print("这是那个旧的的数组：" + "[");
        for (int i = 0; i < oldArr.length; i++) {
            System.out.print(oldArr[i] + " ");
        }
        System.out.println("]");
        for (int i = 0; i < newArr.length; i++) {
            newArr[i] = oldArr[i];
        }
        System.out.print("  这是一个新的数组：" + "[");
        for (int i = 0; i < newArr.length; i++) {
            System.out.print(newArr[i] + " ");
        }
        System.out.println("]");
    }
}
